package cn.itsource.product.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.product.domain.Product;
import cn.itsource.product.domain.ProductDetail;
import cn.itsource.product.query.ProductQuery;
import cn.itsource.product.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务接口
 *    crud
 *    分页+高级
 */
@RestController //@Controller+@ResponseBody
@RequestMapping("/product")
@Api(value = "服务接口",description = "服务接口详细描述")
public class ProductController {
    @Autowired
    private IProductService productService;

    //添加或修改-差异在于是否有id put
    @PutMapping
    @ApiOperation(value = "服务添加或修改",notes = "如果有id就是修改否则就是添加！")
    public AjaxResult addOrUpdate(@RequestBody Product product){
        //通过id判断是添加或者修改
        try {
            if (product.getId()!=null){
                productService.update(product);
            }else {
                productService.add(product);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存失败！"+e.getMessage());
        }

    }
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            productService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败!"+e.getMessage());
        }
    }

    //获取一个 get
    @GetMapping("/{id}") //Get http://localhost:8080/deparment/1 deparment/2
    public Product getById(@PathVariable("id") Long id){
        return productService.getById(id);
    }

    //获取所有 get
    @GetMapping ///product
    public List<Product> getAll(){
        return productService.getAll();
    }

    //分页+高级查询 //post /product 传递对象都往@RequestBody -----后台使用的不用区别是否上架了
    @PostMapping
    public PageList<Product> list(@RequestBody ProductQuery query){
        return productService.queryPage(query);
    }


    //前台只查上架了的
    @PostMapping("/list")
    public PageList<Product> query(@RequestBody ProductQuery query){
        //上架了的---就是一个查询条件
        query.setState(1);
        query.setPageSize(12);
        return productService.queryPage(query);
    }


    //获取一个 get
    @GetMapping("/detail/{productId}") //Get http://localhost:8080/deparment/1 deparment/2
    public ProductDetail getDetailByProductId(@PathVariable("productId") Long productId){
        return productService.getDetailByProductId(productId);
    }

    //上架
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            productService.onsaleOrOffsale(ids,1); //1 表示上架，0表示下架
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("上架失败！"+e.getMessage());
        }

    }

    //下架
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            productService.onsaleOrOffsale(ids,0); //1 表示上架，0表示下架
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("下架失败！"+e.getMessage());
        }

    }
}
